<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/9/17 0017
 * Time: 上午 10:44
 */
namespace app\member\controller;
use app\common\api\member\IMember;
use app\member\validate\Vali_mem;
use app\member\validate\ValidateFun;

class Member implements IMember {
    private static $config = [];
    /**
     * 初始化配置
     * @param array $config
     */
    public function __construct(array $config = [])
    {
        self::$config = array_filter_full(array_merge(self::$config,$config));
    }

    /**
     * 登录
     * @return mixed
     */
    public function login(){
        $data = self::$config;

        #验证参数
        $vali = Vali_mem::login();
        $validate = new ValidateFun($vali['rule'],$vali['message']);
        if (!$validate->check($data)){
            $err = $validate->getError();
            return ['code'=>$err,'msg'=>config("msg.$err")];
        }
        #找出登录这信息
        $info = new \app\member\model\Member();
        $res = $info->login($data);

        unset($res['code'],$res['password'],$res['del']);
        #返回结果
        return ['code'=>200,'msg'=>config('msg.200'),'data'=>$res];
    }

    /**
     * 注册
     * @return mixed
     */
    public function register(){
        $default_data = [
            'phone'     =>'',
            'del'       =>1,
            'createtime'=>date('Y-m-d H:i:s'),
            'updatetime'=>date('Y-m-d H:i:s'),
            'password'  =>'123456',
            'identity'  =>2
        ];
        $data = array_filter_full(array_merge($default_data,self::$config));
        unset($data['id']);

        #验证参数
        $vali = Vali_mem::register();
        $validate = new ValidateFun($vali['rule'],$vali['message']);
        if (!$validate->check($data)){
            $err = $validate->getError();
            return ['code'=>$err,'msg'=>config("msg.{$err}")];
        }

        #数据库数据处理
        $data['code'] =getCode(6);
        $data['password']=getMd5($data['password'],$data['username'],$data['code']);

        #添加用户
        $add =  new \app\member\model\Member();
        $res = $add ->mem_add($data);
        #返回结果
        if ($res>0){
            return ['code'=>200,'msg'=>config('msg.200')];
        }else{
            return ['code'=>10006,'msg'=>config('msg.10006')];
        }
    }

    /**
     * 获取用户详情
     * @return mixed
     */
    public function info(){
        $data = self::$config;
        #验证参数
        $vali = Vali_mem::info();
        $validate = new ValidateFun($vali['rule'],$vali['message']);
        if (!$validate->check($data)){
            $err = $validate->getError();
            return ['code'=>$err,'msg'=>config("msg.$err")];
        }
        #获取信息
        $info = new \app\member\model\Member();
        $res = $info->mem_info($data);
        unset($res['password'],$res['code'],$res['code'],$res['del']);
        #返回结果
        return ['code'=>200,'msg'=>config('msg.200'),'data'=>$res];
    }

    /**
     * 更改用户信息
     * @return mixed
     */
    public function update(){
        $session = session('admin_info');
        $default_data = [
            'id'        => $session['id'],
            'updatetime'=>date('Y-m-d H:i:s')
        ];
        $data = array_filter_full(array_merge($default_data,self::$config));

        #验证参数
        $vali = Vali_mem::info();
        $validate = new ValidateFun($vali['rule'],$vali['message']);
        if (!$validate->check($data)){
            $err = $validate->getError();
            return ['code'=>$err,'msg'=>config("msg.$err")];
        }
        if (isset($data['oldpwd'])||isset($data['newpwd'])){
            $vali = Vali_mem::_info();
            $validate = new ValidateFun($vali['rule'],$vali['message']);
            if (!$validate->check($data)){
                $err = $validate->getError();
                return ['code'=>$err,'msg'=>config("msg.$err")];
            }
        }

        #整理数据库所需的数据
        if (isset($data['newpwd'])){
            $info = new \app\member\model\Member();
            $infos = $info->mem_info($data);
            $data['password'] = getMd5($data['newpwd'],$infos['username'],$infos['code']);
            unset($data['newpwd'],$data['oldpwd']);
        }

        #修改数据
        $update = new \app\member\model\Member();
        $res = $update->mem_update($data);

        #返回结果
        if ($res>0){
            return ['code'=>200,'msg'=>config('msg.200')];
        }else{
            return ['code'=>10007,'msg'=>config('msg.10007')];#数据修改失败
        }
    }

    /**
     * 用户列表
     * @return mixed
     */
    public function user_list(){
        $default =[
            'curr' =>1,
            'limits'=>10,
        ];
        $data = array_filter_full(array_merge($default,self::$config));
        #获取列表
        $list = new \app\member\model\Member();
        $result =$list->mem_list($data);
        #返回结果
        return ['code'=>200,'msg'=>config('msg.200'),'data'=>$result];
    }
}